/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.agent.crm.sechdule.receiver;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.agent.crm.view.R;
import com.pwp.activity.StopScheduleByNotificationActivity;
import com.pwp.dao.ScheduleDAO;
import com.pwp.vo.ScheduleVO;

/**
 * This is an example of implement an {@link BroadcastReceiver} for an alarm that
 * should occur once.
 */
public class RepeatingAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
    	ScheduleVO scheduleVO = (ScheduleVO)intent.getSerializableExtra("schedule");
    	Date d=new Date();
    	ScheduleDAO dao=new ScheduleDAO(context);
    	if("1".equalsIgnoreCase(scheduleVO.getScheduleCycleType())){
  			 dao.resetScheduleStatusFlags(scheduleVO.getScheduleID(),"2");
		    }
		if(d.after(formatStringToDate(scheduleVO.getScheduleEndDate()))){
			Intent intentForStop = new Intent(context,
					RepeatingAlarm.class);
	   		PendingIntent sender = PendingIntent.getBroadcast(context,
	   				scheduleVO.getScheduleID(), intentForStop, 0);
			// And cancel the alarm.
			AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
			am.cancel(sender);
			dao.resetScheduleStatusFlagsToFinished(scheduleVO.getScheduleID());
		}
        Toast.makeText(context,scheduleVO.getScheduleTitle(), Toast.LENGTH_SHORT).show();
        Intent i=new Intent(context,StopScheduleByNotificationActivity.class);
        i.putExtra("scheduleId", scheduleVO.getScheduleID());
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.setData(Uri.parse("custom://"+System.currentTimeMillis()));//这行代码会解决notification传到StopScheduleByNotificationActivity参数不更新问题
        Notification n=new Notification(R.drawable.bg_tab_dial_pressed_up,scheduleVO.getScheduleTitle(), System.currentTimeMillis());
        PendingIntent pIntent=PendingIntent.getActivity(context, 0,i, PendingIntent.FLAG_UPDATE_CURRENT);
        n.defaults=Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;
        n.setLatestEventInfo(context,scheduleVO.getScheduleTitle(),scheduleVO.getScheduleContent(), pIntent);
        NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(scheduleVO.getScheduleID(), n);
    }
    
    private Date formatStringToDate(String str){
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			return format.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			return format.parse(format.format(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new Date();
	}
}



